package com.common.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.hss.common.R;

/**
 * 作者：黄赛赛 on 2015/8/12 10:38
 *
 * @version V1.0
 * @Description: ${todo}()
 */
public class EmptyView extends FrameLayout {
    LinearLayout ll_loading;
    LinearLayout ll_refresh;
    ImageView iv_loading_progress;
    AnimationDrawable ad_loding;

    RefreshListener refreshListener;
    public EmptyView(Context context) {
        super(context);
    }

    public EmptyView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.empty, this);
        ll_loading = (LinearLayout) findViewById(R.id.ll_loading);
        ll_refresh = (LinearLayout) findViewById(R.id.ll_refresh);
        iv_loading_progress = (ImageView) findViewById(R.id.iv_loading_progress);
        ll_refresh.setOnClickListener(btnClick);
        ad_loding = (AnimationDrawable) iv_loading_progress.getDrawable();
    }

    public EmptyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void loading(){
        ll_loading.setVisibility(View.VISIBLE);
        ll_refresh.setVisibility(View.GONE);
        ad_loding.start();
    }

    public void empty(){
        ll_loading.setVisibility(View.GONE);
        ll_refresh.setVisibility(View.VISIBLE);
        if (ad_loding.isRunning()){
            ad_loding.stop();
        }
    }

    OnClickListener btnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != refreshListener){
                refreshListener.refresh();
            }
            loading();
        }
    };

    public static  interface RefreshListener{
        public void refresh();
    }


    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }
}
